// ----------------------------------------
// Filename: my_Timer.c
// Description: The implementation of thread functions
// Author: Jon Maloney
// Date: 9-May-2012
//

//************************************************************
//                    Include .h files
//************************************************************

//************************************************************
//                    Include external references
//************************************************************

//************************************************************
//                    Define Statements
//************************************************************

//************************************************************
//                    Struct, Union, enum Statements
//************************************************************

//************************************************************
//                    Global variables and constants
//************************************************************

//************************************************************
//                    Prototype Functions
//************************************************************

//************************************************************
//                    Implementation
//************************************************************

#include "my_Timers.h"

#include "task.h"

#include <stdio.h>

void __attribute__((__interrupt__, auto_psv)) _T3Interrupt( void )
{
#if 0
	char *pcPrintStatement = "Timer 3 Interrupt\n";
	
	taskENTER_CRITICAL();
	{
		printf( "%s", pcPrintStatement );
		fflush(stdout);
	}
	taskEXIT_CRITICAL();
#endif

	if(AD1CON1bits.ASAM == 1)
	{//An error has occured.
		printf( "ADC Sampling error:- Timer interrupted before sampling completed.");
	}
	
	/* Signal ADC to begin sampling ADC interrupt will handle the data and stop the sampling process*/
	AD1CON1bits.ASAM = 1;		//Starts sampling

	/* Clear the timer interrupt. */
	_T3IF = 0;
}

void vSetupTimer3( unsigned portSHORT usFrequencyHz )
{
	/* T3 is used to generate interrupts.*/
	TMR3 = 0;
	
	T3CONbits.TCS 		= 0;	//Timerx Clock Source Select bit
								//		1 = External clock from pin, TxCK (on the rising edge)
								//		0 = Internal clock (FOSC/2)
	T3CONbits.TCKPS		= 0;	//TimerX Input Clock Prescale Select bits	
								//		11 = 1:256
								//		10 = 1:64
								//		01 = 1:8
								//		00 = 1:1
	T3CONbits.TGATE		= 0;	//TimerY Gated Time Accumulation Enable bit
								//		1 = Gated time accumulation enabled
								//		0 = Gated time accumulation disabled
	T3CONbits.TSIDL		= 0;	//Stop in Idle Mode bit
								//		1 = Discontinue module operation when device enters Idle mode
								//		0 = Continue module operation in Idle mode	
	
	/*Make sure that divide by zero errors do not occur*/
	if(usFrequencyHz == 0)
		return;
	
	/*Slowest frequency we can get with 16 bit timer and 16MHz clock is 244Hz*/
	if(usFrequencyHz <245)
		usFrequencyHz = 244;

	/* Timer is going to interrupt at usFrequencyHz Hz. */
	PR3 = ( unsigned portSHORT ) ( configCPU_CLOCK_HZ / ( unsigned portLONG ) usFrequencyHz );

	/* Setup timer 2 interrupt priority to be above the kernel priority so 
	the timer jitter is not effected by the kernel activity. */
	_T3IP = configKERNEL_INTERRUPT_PRIORITY + 1;

	/* Clear the interrupt as a starting condition. */
	_T3IF = 0;

	/* Enable the timer interrupt*/
	_T3IE = 1;
	
	/* Start the timer. */
	T3CONbits.TON = 1;	//Timer On bit
						//		1 = Starts Timer
						//		0 = Stops Timer	
}
